#!/usr/bin/php -q
<?php
/*
---------------------------------------------------------------------------
salam_check_nt_diskspace for SALAM

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/

foreach ($argv as $value) {
	$temp .= ' ' . $value;
}
$temparr = explode("-", $temp);
foreach ($temparr as $value) {
	list($arg, $data) = explode(" ", $value, 2);
	$args[$arg] = trim($data);
}

$settings_file = dirname($_SERVER['PATH_TRANSLATED']) . "/../library/settings.php";
require($settings_file);

if (isset($args['C']))
	list($criticalgb,$criticalpercent) = explode(",", $args['C']);
else {
	$criticalgb = 0;
	$criticalpercent = 0;
}
if (isset($args['W']))
	list($warninggb,$warningpercent) = explode(",", $args['W']);
else {
	$warninggb = 0;
	$warningpercent = 0;
}

if (isset($args['H']) && $args['H'] != '' && isset($args['D']) && $args['D'] != '') {
	$cmd = 'wmic -U "'. $wmi_user . '"%"' . $wmi_password . '" //' . $args['H'] . ' "Select FreeSpace, Size FROM Win32_LogicalDisk WHERE DeviceID=\'' . $args['D'] . '\'"';
	exec($cmd,$data,$return_code);
	if ($return_code == 0) {
		list($did, $freespace, $size) = explode("|", $data[2], 3);
		$freespace = round($freespace / 1073741824, 2);
		$size = round($size / 1073741824, 2);
		$freepercent = round(($freespace / $size) * 100);
		if ($freespace < $criticalgb || $freepercent < $criticalpercent) {
			echo 'Critical - ' . $args['H'] . ':' . $args['D'] . ' Drive:' . $freespace . ' GB Free, ' . $freepercent . '% Free |freegb=' . $freespace . ' percentfree=' . $freepercent . ' size=' . $size;
			exit(2);
		}
		else if ($freespace < $warninggb || $freepercent < $warningpercent) {
			echo 'Warning - ' . $args['H'] . ':' . $args['D'] . ' Drive:' . $freespace . ' GB Free, ' . $freepercent . '% Free |freegb=' . $freespace . ' percentfree=' . $freepercent . ' size=' . $size;
			exit(1);
		}
		else {
			echo 'OK - ' . $args['H'] . ':' . $args['D'] . ' Drive:' . $freespace . ' GB Free, ' . $freepercent . '% Free |freegb=' . $freespace . ' percentfree=' . $freepercent . ' size=' . $size;
			exit(0);
		}
	}
}
else {
	echo 'ERROR - Missing Arguments|state=3';
	exit(3);
}

?>
